package com.bw.shopping.mvvm.viewmodel;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.bw.mvvm.BaseApplication;
import com.bw.mvvm.arouter.MyARouter;
import com.bw.mvvm.entity.AliEntity;
import com.bw.mvvm.entity.BaseEntity;
import com.bw.mvvm.entity.PaySignEntity;
import com.bw.mvvm.mvvm.model.BaseModel;
import com.bw.mvvm.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm.utils.SpUtils;
import com.bw.my.mvvm.model.entity.ShipAddressEntity;
import com.bw.shopping.R;
import com.bw.shopping.adapter.OrderFormAdapter;
import com.bw.shopping.mvvm.model.OrderFormModel;
import com.bw.shopping.mvvm.model.entity.OrderAmountEntity;
import com.bw.shopping.mvvm.model.entity.SubmitEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class OrderFormVieModel extends BaseViewModel<OrderFormModel> {
    public int count = 0;
    public BaseQuickAdapter adapter = new OrderFormAdapter();
    public ObservableField<String> total = new ObservableField<>();
    public String[] distribution = {"支付配送","在线支付\n快递运输"};
    public String consignee = "选择收货人";
    public ShipAddressEntity.DataBean shipAddressBean;
    public OrderAmountEntity orderAmountEntity;
    public SubmitEntity sub = new SubmitEntity();
    @Override
    protected void result(BaseEntity entity) {
        LogUtils.e(entity.getMessage());
        if (entity instanceof OrderAmountEntity){
            OrderAmountEntity entity1 = (OrderAmountEntity) entity;
            orderAmountEntity = entity1;
            for (int i = 0; i < entity1.getData().getOrderGoodsList().size(); i++) {
                count+= Integer.valueOf(entity1.getData().getOrderGoodsList().get(i).getGoods_price());
            }

            total.set("合计:￥"+count);
            adapter.setNewInstance(entity1.getData().getOrderGoodsList());
        }
        if (entity.getMessage().equals("订单提交成功")){
            HashMap<String,Object> map = new HashMap<>();
            map.put("orderId",sub.data);
            map.put("totalPrice",count);
            RequestBody body = m.createBody(map);
            m.requestPay(body).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this);
        }
        if (entity instanceof PaySignEntity){
            PaySignEntity signEntity = (PaySignEntity) entity;
            AliEntity entity3 = new AliEntity();
            entity3.setCount(count);
            entity3.setOrderId(sub.data);
            entity3.setData(signEntity.data);
            EventBus.getDefault().postSticky(entity3);
            ARouter.getInstance().build(MyARouter.PAYACTIVITY).navigation();
            finishPage();
        }

    }

    @Subscribe(sticky = true)
    public void getOrderById(SubmitEntity submitEntity){
        sub = submitEntity;
        LogUtils.e(submitEntity.data);
        if (submitEntity.data != 0){
            HashMap<String,Object> map = new HashMap<>();
            map.put("orderId",submitEntity.data);
            RequestBody body = m.createBody(map);
            m.requestOrderList(body).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this);
        }
    }



    @Subscribe(sticky = true)
    public void getShipAddress(ShipAddressEntity.DataBean dataBean){
        shipAddressBean = dataBean;
        adapter.removeAllHeaderView();
        View header = LayoutInflater.from(BaseApplication.getInstance()).inflate(R.layout.item_header,null);
        TextView name = header.findViewById(R.id.item_header_name);
        TextView address = header.findViewById(R.id.item_header_address);
        name.setText(dataBean.getShip_user_name());
        address.setText(dataBean.getShip_address());
        adapter.addHeaderView(header);
        adapter.notifyDataSetChanged();
    }

    public void onBack(){
        finishPage();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        total.set("合计:￥0.00");
        EventBus.getDefault().register(this);
    }


    public void submitOrder(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("id",orderAmountEntity.getData().getId());
        map.put("orderGoodsList",orderAmountEntity.getData().getOrderGoodsList());
        map.put("payType",orderAmountEntity.getData().getPayType());
        map.put("shipAddress",shipAddressBean);
        map.put("totalPrice",count);
        HashMap<String,Object> order = new HashMap<>();
        order.put("userId",453);
        order.put("order",map);
        RequestBody body = m.createBody(order);
        m.submitOrder(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
